#!/bin/bash

SVNSERVER=${1}
PROJECT=${2}
FOLDER=${3}
VERSION=${4}
UPDATEVERSION=${5}
echo "$(date +%Y"-"%m"-"%d"_"%k":"%M":"%S)"-----------download $PROJECT-----------  
export JAVA_HOME=/usr/java/jdk1.7.0_79
export M2_HOME=/usr/local/maven
echo server: $SVNSERVER ,  project: $PROJECT  folder: $FOLDER , version: $VERSION 

UPDATEBASE=/tmp/update/bxn-update-base
echo target dir: $UPDATEBASE 
if [ ! -d "$UPDATEBASE" ]; then
	echo ERROR!!!!!!!'$UPDATEBASE folder not found ,exit.' 
	exit -1;
fi
BASE=/tmp/update/$PROJECT

echo tmp base: $BASE , begin download project from svn
rm -rf $BASE
export LC_CTYPE=en_US.UTF-8
if [ "null" = $FOLDER  ];then
	echo FOLDER is null, url is: svn export svn://$SVNSERVER/$PROJECT/$VERSION $BASE  
	svn -q export svn://$SVNSERVER/$PROJECT/$VERSION $BASE --username tiger --password svn555666
else
	echo url is: svn export svn://$SVNSERVER/$PROJECT/$FOLDER/$VERSION $BASE  
	svn -q export svn://$SVNSERVER/$PROJECT/$FOLDER/$VERSION $BASE --username tiger --password svn555666
fi
cd $BASE
sed -i '8c <version>'$VERSION'</version>' pom.xml
if [ -d "/usr/local/maven/repo/cn/boxiao" ];then
	echo rm -rf /usr/local/maven/repo/cn/boxiao
	rm -rf /usr/local/maven/repo/cn/boxiao
fi
JAVA_HOME=/usr/java/jdk1.7.0_79
echo begin to create war
mvn -q package -Dmaven.test.skip
if [ -d "/usr/local/maven/repo/cn/boxiao" ];then
	echo chmod -R 777 /usr/local/maven/repo/cn/boxiao
	chmod -R 777 /usr/local/maven/repo/cn/boxiao
fi
if [ ! -d "target" ]; then
	echo ERROR!!!!!!!'target folder not found ,exit.' 
	rm -rf $BASE
	exit -1;
fi
WAR=$(ls target | grep .war) 
echo war name is: $WAR
if [ ! -f "target/$WAR" ];then
	echo ERROR!!!!!!!'war file is not exist.' 
	rm -rf $BASE
	exit -1;
fi
#unzip war to /tmp/package/temp dir
# tomcat
if [ $PROJECT = "bxn-integration-uic" ];then
	TOMCAT=bx_app/program/tomcat7pf/webapps
	NGINXFILE=uic.conf
	PROPERTIESFILE=uic.properties
	echo $PROJECT: tomcat is $TOMCAT, nginx is $NGINXFILE, properties is $PROPERTIESFILE, $PROPERTIESFILE2, $PROPERTIESFILE3
elif [ $PROJECT = "bxn-core-platform" ];then
	TOMCAT=bx_app/program/tomcat7pf/webapps
	NGINXFILE=platform.conf
	PROPERTIESFILE=platform.properties
	PROPERTIESFILE2=embconfig.properties
	PROPERTIESFILE3=base/common.properties
	echo $PROJECT:s tomcat is $TOMCAT, nginx is $NGINXFILE, properties is $PROPERTIESFILE, $PROPERTIESFILE2, $PROPERTIESFILE3
elif [ $PROJECT = "cas-server" ];then
	TOMCAT=bx_app/program/tomcat7pf/webapps
	NGINXFILE=sso.conf
	PROPERTIESFILE=cas.properties
	echo $PROJECT: tomcat is $TOMCAT, nginx is $NGINXFILE, properties is $PROPERTIESFILE, $PROPERTIESFILE2, $PROPERTIESFILE3
elif [ $PROJECT = "bxn-score" ];then
	TOMCAT=bx_app/program/tomcat7apps1/webapps
	NGINXFILE=score.conf
	PROPERTIESFILE=score.properties
	echo $PROJECT: tomcat is $TOMCAT, nginx is $NGINXFILE, properties is $PROPERTIESFILE, $PROPERTIESFILE2, $PROPERTIESFILE3
elif [ $PROJECT = "bxn-timetable" ];then
	TOMCAT=bx_app/program/tomcat7apps1/webapps
	NGINXFILE=timetable.conf
	PROPERTIESFILE=timetable.properties
	echo $PROJECT: tomcat is $TOMCAT, nginx is $NGINXFILE, properties is $PROPERTIESFILE, $PROPERTIESFILE2, $PROPERTIESFILE3
elif [ $PROJECT = "bxn-microsite" ];then
	TOMCAT=bx_app/program/tomcat7apps1/webapps
	NGINXFILE=microsite.conf
	PROPERTIESFILE=microsite.properties
	echo $PROJECT: tomcat is $TOMCAT, nginx is $NGINXFILE, properties is $PROPERTIESFILE, $PROPERTIESFILE2, $PROPERTIESFILE3
elif [ $PROJECT = "bxn-portal" ];then
	TOMCAT=bx_app/program/tomcat7apps2/webapps
	NGINXFILE=portal.conf
	PROPERTIESFILE=portal.properties
	PROPERTIESFILE2=portal-cms.properties
	echo $PROJECT: tomcat is $TOMCAT, nginx is $NGINXFILE, properties is $PROPERTIESFILE, $PROPERTIESFILE2, $PROPERTIESFILE3
elif [ $PROJECT = "bxn-integration-office" ];then
	TOMCAT=bx_app/program/tomcat7apps2/webapps
	NGINXFILE=office.conf
	PROPERTIESFILE=office.properties
	echo $PROJECT: tomcat is $TOMCAT, nginx is $NGINXFILE, properties is $PROPERTIESFILE, $PROPERTIESFILE2, $PROPERTIESFILE3
elif [ $PROJECT = "bxn-static-resource" ];then
	TOMCAT=bx_app/program/static
	PROPERTIESFILE=xxx.properties
	echo $PROJECT: tomcat is $TOMCAT, nginx is $NGINXFILE, properties is $PROPERTIESFILE, $PROPERTIESFILE2, $PROPERTIESFILE3
else
	echo ERROR!!!!!!!'project is not match ,exit.'
	exit -1;
fi

CONTEXTPATH=${WAR%.war*}
BX_CONFIG=bx_app/config
BX_FDATA=bx_app/f_data
NGINXCONFPATH=bx_app/program/nginx/nginx-1.7.0/conf/module
BX_DATA=bx_app/data/update/$UPDATEVERSION/$CONTEXTPATH
# data : all are in dirs
if [ ! -d "$UPDATEBASE/$BX_DATA" ];then
	mkdir $UPDATEBASE/$BX_DATA
fi
echo mv target/$WAR to $TOMCAT
mv target/$WAR $UPDATEBASE/$TOMCAT
if [ -d "script/update/$UPDATEVERSION" ];then
	echo mv script/update/files to $BX_DATA
	mv script/update/$UPDATEVERSION/* $UPDATEBASE/$BX_DATA
else
	echo $PROJECT: has no script to update.
fi
if [ -d "doc/f_data" ];then
	echo cp f_data to $BX_FDATA
	sh /usr/packagetools/mvfiles.sh doc/f_data $UPDATEBASE/$BX_FDATA
else
	echo $PROJECT: f_data is not exits.
fi
# nginx
if [ -f doc/$NGINXFILE ];then
	echo mv doc/$NGINXFILE $NGINXCONFPATH
	mv doc/$NGINXFILE $UPDATEBASE/$NGINXCONFPATH
else
	echo $PROJECT has no nginx file.
fi
cd $UPDATEBASE/$TOMCAT
rm -rf $CONTEXTPATH
echo unzip $WAR -d $CONTEXTPATH
unzip -q $WAR -d $CONTEXTPATH
echo remove $WAR finished
rm -rf $WAR 
if [ $PROJECT = "bxn-static-resource" ] || [ $PROJECT = "cas-server" ];then
	echo $PROJECT has no classes
else
	echo begin jars: $PROJECT-$VERSION.jar
	cd $UPDATEBASE/$TOMCAT/$CONTEXTPATH/WEB-INF/classes
	jar cvf  $PROJECT-$VERSION.jar *
	mv $PROJECT-$VERSION.jar ../lib/
	rm -rf ../classes
fi
# properties
if [ -d "$UPDATEBASE/$TOMCAT/$CONTEXTPATH/WEB-INF" ];then
	echo cd $UPDATEBASE/$TOMCAT/$CONTEXTPATH/WEB-INF
	cd $UPDATEBASE/$TOMCAT/$CONTEXTPATH/WEB-INF
	if [ -f application/$PROPERTIESFILE ];then
		echo cp application/$PROPERTIESFILE $UPDATEBASE/$BX_CONFIG
		cp application/$PROPERTIESFILE $UPDATEBASE/$BX_CONFIG
	elif [ -f $PROPERTIESFILE ];then
		echo cp $PROPERTIESFILE $UPDATEBASE/$BX_CONFIG
		cp $PROPERTIESFILE $UPDATEBASE/$BX_CONFIG
	else	
		echo $PROJECT has no properties file.
	fi
	if [ -f application/$PROPERTIESFILE2 ];then
		echo cp application/$PROPERTIESFILE2 $UPDATEBASE/$BX_CONFIG
		cp application/$PROPERTIESFILE2 $UPDATEBASE/$BX_CONFIG
	fi
	if [ -f application/$PROPERTIESFILE3 ];then
		echo cp application/$PROPERTIESFILE3 $UPDATEBASE/$BX_CONFIG
		cp application/$PROPERTIESFILE3 $UPDATEBASE/$BX_CONFIG
	fi
else
	echo $PROJECT has no WEB-INF
fi
# version.txt
version_dir="$UPDATEBASE/$TOMCAT/$CONTEXTPATH/dynamic"
if [ ! -d $version_dir ];then
	echo ERROR! not found dynamic dir, will mkdir
	mkdir $version_dir
	mkdir $version_dir/resources
fi
touch $version_dir/resources/version.txt
echo $VERSION >> $version_dir/resources/version.txt
echo rm -rf $BASE
rm -rf $BASE
